package tongxin1;

public class test1 {
    public static void main(String[] args) {
        Print1 p = new Print1();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (Print1.class){
                        try {
                            p.printOne();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (Print1.class){
                        try {
                            p.printTwo();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }
}
class Print1{
     int flag = 1;
    public void printOne() throws InterruptedException {
        System.out.println(flag+ "one");

        if(flag != 1){
            Print1.class.wait();
        }
        System.out.print("1");
        System.out.print("2");
        System.out.print("3");
        System.out.println();
        flag = 2;

        Print1.class.notify();

    }
    public void printTwo() throws InterruptedException {
        System.out.println(flag+ "two");

        if(flag != 2){
            Print1.class.wait();
        }
        System.out.print("a");
        System.out.print("b");
        System.out.print("c");
        System.out.println();
        flag = 1;
        Print1.class.notify();
    }
}
